<template>
  <div style="height:100%">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-row :gutter="2">
        <el-col :span="12">
          <el-form-item label="产品编号" prop="busMainData.itemId">
            <BaseItemSelectDialog v-model="form.busMainData.itemCode" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" @itemSelectCallback="prodItemSelectCallback" />
          </el-form-item>
          <el-form-item label="MRP计算数" prop="busMainData.mrpQty">
            <el-input-number v-model.number="form.busMainData.mrpQty" :controls="false" :precision="0" :min="1" size="mini"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产部门" prop="busMainData.sysOrgCode">
            <OrgsetTreeSelect v-model="form.busMainData.sysOrgCode" :editable="form.busPageInfo.editable && form.busMainData.funProcess === '10'" :has-priv="false" style="width: 200px" />
          </el-form-item>
          <el-form-item label="模号" prop="busMainData.mouldNo">
            <el-input v-model="form.busMainData.mouldNo" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="2">
      <el-col :span="24">
        <el-table
          :data="form.bomDetails"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :default-expand-all="true"
          size="mini"
          highlight-current-row
          stripe
          border
          max-height="400px"
          row-key="id"
          style="width: 100%">
          <el-table-column v-if="form.busPageInfo.editable" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="addSubBomItem(scope.$index, scope.row)">添加</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
          <el-table-column label="物料编号" width="280">
            <template slot-scope="scope">
              <BaseItemSelectDialog v-if="form.busPageInfo.editable" v-model="scope.row.itemCode" :param-data="scope.row" @itemSelectCallback="bomItemSelectCallback" />
              <span v-else>{{ scope.row.itemCode }}</span>
            </template>
          </el-table-column>
          <el-table-column label="物料名称" prop="itemName" width="100" show-overflow-tooltip/>
          <el-table-column label="规格" prop="itemSpec" width="100" show-overflow-tooltip />
          <el-table-column label="库存单位" width="90">
            <template slot-scope="scope">
              <template v-for="item in dicts.item_unit">
                <span v-if="scope.row.stockUnit && scope.row.stockUnit === item.dictCode" :key="item.id" >{{ item.dictName }}</span>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="使用量" width="100">
            <template slot="header" slot-scope="scope">
              使用量<IconHelp display-content="用量＝使用量+使用量*损耗率+使用量*固定损耗率+固定损耗数" />
            </template>
            <template slot-scope="scope">
              <el-input-number v-if="form.busPageInfo.editable" v-model.number="scope.row.reqQty" :controls="false" :precision="2" :min="0" size="mini" style="width: 90px"/>
              <span v-else>{{ scope.row.reqQty }}</span>
            </template>
          </el-table-column>
          <el-table-column label="损耗率" width="100">
            <template slot-scope="scope">
              <el-input-number v-if="form.busPageInfo.editable" v-model.number="scope.row.scrapRate" :controls="false" :precision="2" :min="0" size="mini" style="width: 90px"/>
              <span v-else>{{ scope.row.scrapRate }}</span>
            </template>
          </el-table-column>
          <el-table-column label="固定损耗率" width="100">
            <template slot-scope="scope">
              <el-input-number v-if="form.busPageInfo.editable" v-model.number="scope.row.fixScrapRate" :controls="false" :precision="2" :min="0" size="mini" style="width: 90px"/>
              <span v-else>{{ scope.row.fixScrapRate }}</span>
            </template>
          </el-table-column>
          <el-table-column label="固定损耗数" width="100">
            <template slot-scope="scope">
              <el-input-number v-if="form.busPageInfo.editable" v-model.number="scope.row.fixScrapQty" :controls="false" :precision="2" :min="0" size="mini" style="width: 90px"/>
              <span v-else>{{ scope.row.fixScrapQty }}</span>
            </template>
          </el-table-column>
          <el-table-column label="物料属性" width="120">
            <template slot-scope="scope">
              <el-select :disabled="!form.busPageInfo.editable" v-model="scope.row.itemType" placeholder="物料属性">
                <el-option
                  v-for="(item) in dicts.item_type"
                  :key="item.id"
                  :label="item.dictName"
                  :value="item.dictCode"/>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="备注">
            <template slot-scope="scope">
              <el-input v-if="form.busPageInfo.editable" v-model="scope.row.rmk" />
              <span v-else>{{ scope.row.rmk }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col v-if="form.busPageInfo.editable" :span="24">
        <div class="el-table-add-row" style="width: 99.2%;" @click="addBomItem"><span>+ 添加</span></div>
      </el-col>
    </el-row>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','BASEBOM_ALL','BASEBOM_CHANGE','BASEBOM_CREATE']" :loading="loading" type="primary" @click="doSave">暂存</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','BASEBOM_ALL','BASEBOM_SUBMIT']" :loading="loading" type="primary" @click="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import baseBom from '@/api/base/BaseBom'
import BaseItemSelectDialog from '@/views/components/BaseItemSelectDialog'
import OrgsetTreeSelect from '@/views/components/OrgsetTreeSelect'

export default {
  components: { BaseItemSelectDialog, OrgsetTreeSelect },
  dictTypes: ['item_type', 'item_unit'],
  props: {
  },
  data() {
    return {
      loading: false,
      style: 'width: 184px',
      form: {
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        bomDetails: [],
        busPageInfo: {
          editable: false
        }
      },
      rules: {
      }
    }
  },
  mounted() {
    this.$emit('afterMounted')
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    getBomDetailData(bomId, replaceIdFlag) {
      if (bomId) {
        baseBom.getBomDetailData(bomId, replaceIdFlag).then(res => {
          this.form.bomDetails = res.data
        })
      }
    },
    viewApproveData(busId) {
      const data = { paramBean: { busId: busId }}
      baseBom.toViewApprove(data).then(res => {
        this.form = res.data
        this.getBomDetailData(this.form.busMainData.id, false)
      })
    },
    prodItemSelectCallback(selectedRow, selectObj) {
    },
    bomItemSelectCallback(selectedRow, selectObj, paramData) {
    }
  }
}
</script>
